package com.yyge.controller.mobile.cart;


import com.yyge.common.controller.BaseController;
import com.yyge.common.vo.resp.R;
import com.yyge.controller.mobile.cart.req.MobileCartAddReq;
import com.yyge.controller.mobile.cart.resp.MobileAddCartNumReq;
import com.yyge.controller.mobile.cart.resp.MobileCartResp;
import com.yyge.controller.mobile.category.resp.MobileCategoryResp;
import com.yyge.controller.mobile.mapstruct.MobileCartConvert;
import com.yyge.controller.mobile.product.MobileProductController;
import com.yyge.entity.Employee;
import com.yyge.entity.ShoppingCart;
import com.yyge.mapper.ShoppingCartMapper;
import com.yyge.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "手机端-购物车")
@RestController
@RequestMapping(value = "/mobile/cart")
public class MobileCartController extends BaseController<Employee,MobileCartResp> {
    public MobileCartController(){
        this.converter=MobileCartConvert.class;
    }
    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation(value = "查询当前用户商家的购物车")
    @GetMapping(value = "/merchantId/{id}")
    public R<List<MobileCartResp>> findByMerchantId(@PathVariable(value = "id") Long id){
        List<MobileCartResp> list =this.shoppingCartService.findByMerchantId(id);
        return R.okHasData(list);
    }


    @ApiOperation(value = "首次产品加入购物车")
    @PostMapping(value = "/add")
    public R cartAdd(@RequestBody @Valid MobileCartAddReq req){
        System.out.println("Contrillers是否有对象");
        System.out.println(req);
        Integer num=this.shoppingCartService.cartAdd(req);
        //创建一个map
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number", num);
        return  R.okHasData(dataMap);
    }

    @ApiOperation(value = "向已有购物车商品增加数量")
    @PostMapping(value = "/addnum")
    public R addCarNumApi(@RequestBody @Valid MobileAddCartNumReq req) throws Exception {

        Employee entity = this.getEntity(req);
        System.out.println(entity.getClass());
        System.out.println("------");

        Integer num=this.shoppingCartService.addCarNumApi(req);
        //创建一个map
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number", num);
        return  R.okHasData(dataMap);
    }

    @ApiOperation(value = "向已有购物车商品增加数量")
    @GetMapping(value = "/sub/{dishFlavorId}")
    public R subCarNumApi(@PathVariable("dishFlavorId")  Long dishFlavorId){
        Integer num=this.shoppingCartService.subCarNumApi(dishFlavorId);
        //创建一个map
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number", num);
        return  R.okHasData(dataMap);
    }

}
